package com.hxd.otc.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.StringRes;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.flyco.dialog.widget.base.BottomBaseDialog;
import com.hxd.otc.R;
import com.hxd.otc.api.Constant;
import com.hxd.otc.utils.ResourceUtil;
import com.hxd.otc.widget.BlocksToast;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lzy on 2018/6/9 0009.
 * description ${
 * <p>
 * Changed by somebody on 2018/6/9 0009
 * description ${TODO}
 */

public class ConfirmPaymentDialog extends BottomBaseDialog<ConfirmPaymentDialog> {
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.tv_message)
    TextView mTvMessage;
    @BindView(R.id.et_money)
    EditText mEtPassword;
    @BindView(R.id.tv_require)
    TextView mTvRequire;
    @BindView(R.id.view_hor_line)
    View viewHorLine;
    @BindView(R.id.view_ver_line)
    View viewVerLine;
    @BindView(R.id.tv_cancel)
    TextView mTvCancel;
    @BindView(R.id.tv_confirm)
    TextView mTvConfirm;

    private boolean isChecked;
    private CharSequence mUnCheckedWarning;
    private CharSequence mEmptyContentWarning;
    private CharSequence title;
    private CharSequence message;
    private CharSequence editTextHint;
    private CharSequence require;
    private OnConfirmClickListener onConfirmClickListener;
    private int inputType = -1;

    public ConfirmPaymentDialog(Context context) {
        super(context);
    }

    @Override
    public View onCreateView() {
        return View.inflate(getContext(), R.layout.dialog_confirm_payment, null);
    }

    @Override
    public void onViewCreated(View view) {
        ButterKnife.bind(this, view);
    }

    @Override
    public void setUiBeforShow() {
        mTvTitle.setText(title);
        mTvMessage.setText(message);
//        mEtPassword.setHint(editTextHint);
        mTvRequire.setText(require);
        mTvRequire.setSelected(isChecked);
    }

    @OnClick({R.id.tv_require, R.id.tv_cancel, R.id.tv_confirm})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_require:
                mTvRequire.setSelected(!mTvRequire.isSelected());
                break;
            case R.id.tv_cancel:
                dismiss();
                break;
            case R.id.tv_confirm:
                onConfirm();
                break;
        }
    }

    private void onConfirm() {
        if (!mTvRequire.isSelected() && !TextUtils.isEmpty(mUnCheckedWarning)) {
            BlocksToast.get().showToast(mUnCheckedWarning.toString());
            return;
        }
//        String password = mEtPassword.getText().toString().trim();
//        if (TextUtils.isEmpty(password) && !TextUtils.isEmpty(mEmptyContentWarning)) {
//            BlocksToast.get().showToast(mEmptyContentWarning.toString());
//            return;
//        }
//
//        if (password.length() < Constant.PAY_PASSWORD_MIN_LENGTH) {
//            BlocksToast.get().showToast(String.format(ResourceUtil.getString(R.string.my_min_length_of_pay_password), Constant.PAY_PASSWORD_MIN_LENGTH));
//            return;
//        }

        if (onConfirmClickListener == null) return;
        onConfirmClickListener.onConfirm(this, "");
    }

    /**
     * 设置标题
     */
    public ConfirmPaymentDialog setTitleText(CharSequence title) {
        this.title = title;
        return this;
    }

    /**
     * 设置标题
     */
    public ConfirmPaymentDialog setTitleText(@StringRes int resid) {
        setTitleText(getString(resid));
        return this;
    }

    /**
     * 设置提示内容
     */
    public ConfirmPaymentDialog setMessage(CharSequence message) {
        this.message = message;
        return this;
    }

    /**
     * 设置提示内容
     */
    public ConfirmPaymentDialog setMessage(@StringRes int resid) {
        setMessage(getString(resid));
        return this;
    }

    /**
     * 设置输入框的提示文本
     */
    public ConfirmPaymentDialog setEditTextHint(CharSequence editTextHint) {
        this.editTextHint = editTextHint;
        return this;
    }

    /**
     * 设置输入框的提示文本
     */
    public ConfirmPaymentDialog setEditHint(@StringRes int resid) {
        setEditTextHint(getString(resid));
        return this;
    }

    /**
     * 设置同意协议内容
     */
    public ConfirmPaymentDialog setRequire(CharSequence require) {
        this.require = require;
        return this;
    }

    /**
     * 设置同意协议内容
     */
    public ConfirmPaymentDialog setRequire(@StringRes int resid) {
        setRequire(getString(resid));
        return this;
    }

    /**
     * 设置同意协议，点击确定的提示文本，当不设置时，不进行任何操作
     */
    public ConfirmPaymentDialog setUncheckedWarning(CharSequence uncheckedWarning) {
        mUnCheckedWarning = uncheckedWarning;
        return this;
    }

    /**
     * 设置同意协议，点击确定的提示文本，当不设置时，不进行任何操作
     */
    public ConfirmPaymentDialog setUncheckedWarning(@StringRes int resid) {
        setUncheckedWarning(getString(resid));
        return this;
    }

    /**
     * 设置点击确定是，输入框内容为空的提示文本，当不设置时，不进行任何操作
     */
    public ConfirmPaymentDialog setEmptyContentWarning(CharSequence emptyContentWarning) {
        mEmptyContentWarning = emptyContentWarning;
        return this;
    }

    /**
     * 设置点击确定是，输入框内容为空的提示文本，当不设置时，不进行任何操作
     */
    public ConfirmPaymentDialog setEmptyContentWarning(@StringRes int resid) {
        setEmptyContentWarning(getString(resid));
        return this;
    }

    public String getEtPsWordStr() {
        return mEtPassword.getText().toString();
    }

    /**
     * 设置默认选中状态
     */
    public ConfirmPaymentDialog setChecked(boolean isChecked) {
        this.isChecked = isChecked;
        return this;
    }

    /**
     * 设置确认回调监听
     */
    public ConfirmPaymentDialog setOnConfirmClickListener(OnConfirmClickListener onConfirmClickListener) {
        this.onConfirmClickListener = onConfirmClickListener;
        return this;
    }

    /**
     * 设置输入框输入值类型
     */
    public ConfirmPaymentDialog setInputType(int type) {
        inputType = type;
        return this;
    }

    public interface OnConfirmClickListener {
        /**
         * 根据实现方式决定关闭对话框时机
         *
         * @param dialog   用于关闭对话框等操作
         * @param password 输入框的密码
         */
        void onConfirm(Dialog dialog, String password);
    }

    private CharSequence getString(@StringRes int resid) {
        return getContext().getString(resid);
    }
}
